package com.pingan.haofang.searchcloud.common.configuration;
import java.nio.charset.Charset;
import java.util.Collections;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

/**
 * rpc配置
 * 
 * @author LUYI374
 * @date 2017年4月6日
 * @since 1.0.0
 */
@Configuration
public class RpcConfiguration {

    @SuppressWarnings("rawtypes")
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
        converter.setDefaultCharset(Charset.forName("UTF-8"));
        
        restTemplate.getMessageConverters().add(converter);
        for(HttpMessageConverter converterTemp :restTemplate.getMessageConverters()){
            if(converterTemp instanceof AbstractHttpMessageConverter){
                AbstractHttpMessageConverter absconverter = (AbstractHttpMessageConverter)converterTemp;
                absconverter.setDefaultCharset(Charset.forName("UTF-8"));
            }
        }
        return restTemplate;
    }
    
}
